package com.demo.statemachine.demos.config;

import com.demo.statemachine.demos.event.OrderEvents;
import com.demo.statemachine.demos.state.OrderStates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.statemachine.StateMachine;
import org.springframework.stereotype.Component;

@Component
public class StateMachineRunner implements CommandLineRunner {

    @Autowired
    private StateMachine<OrderStates, OrderEvents> stateMachine;

    @Override
    public void run(String... args) throws Exception {
        // 设置初始变量，比如发起审核人的ID
        stateMachine.getExtendedState().getVariables().put("userId", "12345");
        
        stateMachine.start();
        stateMachine.sendEvent(OrderEvents.CREATE);
        stateMachine.sendEvent(OrderEvents.START_REVIEW);
        stateMachine.sendEvent(OrderEvents.APPROVE); // 或者 stateMachine.sendEvent(OrderEvents.REJECT);
    }
}
